#region using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.PictureAcquisition;
#endregion
namespace PictureAcquisitionApplication
{
    public partial class FrmPictureAcquisition : Form
    {
        #region Globals
        PictureAcquisitionManager pictureAcquisitionManager = new PictureAcquisitionManager();
        DeviceInfo deviceInfo = new DeviceInfo();
        DeviceDetails deviceDetails = new DeviceDetails();
        #endregion

        #region Constructor
        public FrmPictureAcquisition()
        {
            InitializeComponent();
            FrmPictureAcquisitionInitialize();
        }
        #endregion

        #region Form Initialization
        private void FrmPictureAcquisitionInitialize()
        { 
            rdbtnAcquisition.Checked = true;
            chkbxAllDevice.Checked = true;
            chkbxSupressProgressDialog.Checked = false;
            RdbtnDeviceSelectionDialogChecked();
            rdbtnDeviceInfo.Checked = false;
            rdbtnSettingsDialog.Checked = false;
            rdbtnDeviceItems.Checked = false;

        }
        #endregion

        #region Radio Button Checked And Unchecked Method
        private void RdbtnDeviceSelectionDialogChecked()
        {
            
            chkbxAllDevice.Enabled = true;
            if (chkbxAllDevice.Checked)
            {
                pnlDeviceSelectionOptions.Enabled = false;
                chkbxWIAcameras.Checked = true;
                chkbxWIAscanners.Checked = true;
                chkbxSTIdevices.Checked = true;
                chkbxFSdevices.Checked = true;
                chkbxCardRecorderDevice.Checked = true;
                chkbxWPDdevices.Checked = true;
                chkbxDigitalVideoCameras.Checked = true;
                chkbxOfflineDevices.Checked = true;
            }
            else
            {
                pnlDeviceSelectionOptions.Enabled = true;
                chkbxWIAcameras.Checked = false;
                chkbxWIAscanners.Checked = false;
                chkbxSTIdevices.Checked = false;
                chkbxFSdevices.Checked = false;
                chkbxCardRecorderDevice.Checked = false;
                chkbxWPDdevices.Checked = false;
                chkbxDigitalVideoCameras.Checked = false;
                chkbxOfflineDevices.Checked = false;
            }

            pnlAcquisitionControl.Enabled = true;
            if (chkbxTargetLocation.Checked)
            {
                chkbxTargetLocation.Checked = true;
                tbxTargetLocation.Enabled = true;
            }
            else
            {
                chkbxTargetLocation.Checked = false;
                tbxTargetLocation.Enabled = false;
            }

            if (chkbxSupressProgressDialog.Checked)
                chkbxSupressProgressDialog.Checked = true;
            else
                chkbxSupressProgressDialog.Checked = false;
            
        }

        private void RdbtnDeviceSelectionDialogUnchecked()
        {
            pnlDeviceSelectionOptions.Enabled = false;
            chkbxAllDevice.Enabled = false;

            if(chkbxAllDevice.Checked)
                chkbxAllDevice.Checked = true;
            else
                chkbxAllDevice.Checked = false;         

            if(chkbxWIAcameras.Checked)
                chkbxWIAcameras.Checked = true;
            else
                chkbxWIAcameras.Checked = false;

            if (chkbxWIAscanners.Checked)
                chkbxWIAscanners.Checked = true;
            else
                chkbxWIAscanners.Checked = false;

            if (chkbxSTIdevices.Checked)
                chkbxSTIdevices.Checked = true;
            else
                chkbxSTIdevices.Checked = false;

            if(chkbxFSdevices.Checked)
                chkbxFSdevices.Checked = true;
            else
                chkbxFSdevices.Checked = false;

            if(chkbxCardRecorderDevice.Checked )
                chkbxCardRecorderDevice.Checked = true;
            else
                chkbxCardRecorderDevice.Checked = false;

            if(chkbxWPDdevices.Checked) 
                chkbxWPDdevices.Checked = true;
            else
                chkbxWPDdevices.Checked = false;

            if(chkbxDigitalVideoCameras.Checked)
                chkbxDigitalVideoCameras.Checked = true;
            else
                chkbxDigitalVideoCameras.Checked = false;
            
            if(chkbxOfflineDevices.Checked)
                chkbxOfflineDevices.Checked = true;
            else
                chkbxOfflineDevices.Checked = false;

            pnlAcquisitionControl.Enabled = false;
            if (chkbxTargetLocation.Checked)
                chkbxTargetLocation.Checked = true;
            else
                chkbxTargetLocation.Checked = false;

            if(chkbxSupressProgressDialog.Checked)
                chkbxSupressProgressDialog.Checked = true;
            else
                chkbxSupressProgressDialog.Checked = false;
        }
        #endregion     

        #region Helper Methods
        private void DeviceSelectionDialogOperation()
        {
            List<DeviceFlag> listOfDeviceFlags = new List<DeviceFlag>();
            bool hImport = false;
            bool showProcessDialog = false;
            DeviceFlag flag = DeviceFlag.None;
            try
            {
                if (!chkbxSupressProgressDialog.Checked)
                    showProcessDialog = true;

                if (chkbxAllDevice.Checked)
                    hImport = pictureAcquisitionManager.ShowAllDeviceDialog(this.Handle,"Devices Connected", "Import");
                else 
                {
                    if(chkbxCardRecorderDevice.Checked | chkbxDigitalVideoCameras.Checked 
                        | chkbxFSdevices.Checked | chkbxOfflineDevices.Checked 
                        | chkbxSTIdevices.Checked | chkbxWIAcameras.Checked 
                        | chkbxWIAscanners.Checked | chkbxWPDdevices.Checked) 
                    {
                        if (chkbxCardRecorderDevice.Checked)
                            flag = DeviceFlag.CardReaders;
                        if (chkbxDigitalVideoCameras.Checked)
                            flag = flag | DeviceFlag.DigitalVideoCameras;
                        if (chkbxFSdevices.Checked)
                            flag = flag | DeviceFlag.FS;
                        if (chkbxOfflineDevices.Checked)
                            flag = flag | DeviceFlag.OffLine;
                        if (chkbxSTIdevices.Checked)
                            flag = flag | DeviceFlag.Sti;
                        if (chkbxWIAcameras.Checked)
                            flag = flag | DeviceFlag.Cameras;
                        if (chkbxWIAscanners.Checked)
                            flag = flag | DeviceFlag.Scanners;
                        if (chkbxWPDdevices.Checked)
                            flag = flag | DeviceFlag.Wpd;                      
                    }

                    if (flag == DeviceFlag.None)
                        MessageBox.Show("Please select atleast one device!", "Picture Acquisition",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        hImport = pictureAcquisitionManager.ShowDeviceSelectionDialog
                          (this.Handle,flag, "Devices Connected", "Import");

                }
                if (hImport == true)
                {
                    if (!tbxTargetLocation.Enabled)
                    {
                        deviceInfo = pictureAcquisitionManager.GetSelectedDevice();
                        pictureAcquisitionManager.AcquireItems(deviceInfo.Id, showProcessDialog);
                    }
                    else
                    {
                        deviceInfo = pictureAcquisitionManager.GetSelectedDevice();
                        pictureAcquisitionManager.AcquireItems(deviceInfo.Id, showProcessDialog, tbxTargetLocation.Text);
                    }

                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

        }
        private void DeviceInfoOperation()
        {
            bool hImport = true;
            try
            {
                hImport = pictureAcquisitionManager.ShowAllDeviceDialog(this.Handle,"Devices Connected", "Get Device Info");
                if (hImport == true)
                {
                    deviceInfo = pictureAcquisitionManager.GetSelectedDevice();
                    new DeviceDetails(deviceInfo).ShowDialog();                                   
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void DeviceItemsOperation()
        {
            bool hImport = true;
            try
            {
                hImport = pictureAcquisitionManager.ShowAllDeviceDialog(this.Handle,"Devices Connected", "Get Items");
                if (hImport == true)
                {
                    deviceInfo = pictureAcquisitionManager.GetSelectedDevice();
                    new DeviceItemsDetails(pictureAcquisitionManager, deviceInfo.Id.ToString()).ShowDialog();
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void DeviceItemsInThumbnailsOperation()
        {
            bool hImport = true;
            try
            {
                hImport = pictureAcquisitionManager.ShowAllDeviceDialog(this.Handle, "Devices Connected", "Get Items");
                if (hImport == true)
                {
                    deviceInfo = pictureAcquisitionManager.GetSelectedDevice();
                    new DeviceItemsDetailsInThumbnails(pictureAcquisitionManager, deviceInfo.Id.ToString()).ShowDialog();
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }
        #endregion

        #region EventHandler
        private void rdbtnDeviceSelectionDialog_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbtnAcquisition.Checked == true)
            {
                chkbxAllDevice.Checked = true;
                RdbtnDeviceSelectionDialogChecked();
            }
            else
                RdbtnDeviceSelectionDialogUnchecked();

        }

        private void chkbxAllDevice_CheckedChanged(object sender, EventArgs e)
        {
            RdbtnDeviceSelectionDialogChecked();
        }

        private void rdbtnDeviceInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbtnDeviceInfo.Checked == true)
                RdbtnDeviceSelectionDialogUnchecked();
        }

        private void rdbtnSettingsDialog_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbtnSettingsDialog.Checked == true)
                RdbtnDeviceSelectionDialogUnchecked();
        }

        private void rdbtnDeviceItemDetailsInThumbnail_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbtnDeviceItemDetailsInThumbnail.Checked)
                RdbtnDeviceSelectionDialogUnchecked();
        }

        private void rdbtnDeviceItems_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbtnDeviceItems.Checked)
                RdbtnDeviceSelectionDialogUnchecked();
        }
        private void chkbxCustomFilenameTemplate_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTargetLocation.Checked)
                tbxTargetLocation.Enabled = true;
            else
                tbxTargetLocation.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (rdbtnAcquisition.Checked)
                    DeviceSelectionDialogOperation();
                else if (rdbtnDeviceInfo.Checked)
                    DeviceInfoOperation();
                else if (rdbtnSettingsDialog.Checked)
                    pictureAcquisitionManager.ShowImportSettings();
                else if (rdbtnDeviceItems.Checked)
                    DeviceItemsOperation();
                else if (rdbtnDeviceItemDetailsInThumbnail.Checked)
                    DeviceItemsInThumbnailsOperation();        
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion
    }
}